package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.mapper.EmployeeMapper;
import com.itheima.reggie.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

/**
 * @author Xiao
 * @date 2021/9/21
 */
@Service
@Slf4j
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements EmployeeService {
    /**
     * 后台管理-登录
     * @param employee
     * @return
     */
    @Override
    public Employee login(Employee employee) {
        //① 将页面提交的密码password进行md5加密处理
        String password = employee.getPassword();
        //② 根据页面提交的用户名username和加密后的password查询数据库
        password = DigestUtils.md5DigestAsHex(employee.getPassword().getBytes());
        LambdaQueryWrapper<Employee> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Employee::getUsername,employee.getUsername())
                .eq(Employee::getPassword,password);
        Employee emp = this.getOne(wrapper);
        return emp;
    }

    /**
     * 员工管理-添加员工
     * @param employee
     * @return
     */
    @Override
    public boolean saveEmp(Employee employee) {
        //查询用户名是否存在 --> 添加全局异常处理器，无须查询
        /*String username = employee.getUsername();
        boolean exists = employeeService.selectEmpByUsername(username);
        if (exists) {
            return R.error("用户名已存在！");
        }*/
        //设置页面未填充的字段信息 --> 公共字段由自定义对象处理器和@TableFild注解实现自动填充
        /*Long empId = (Long) request.getSession().getAttribute("loginEmp");
        employee.setCreateUser(empId);
        employee.setUpdateUser(empId);
        employee.setCreateTime(LocalDateTime.now());
        employee.setUpdateTime(LocalDateTime.now());*/
        //设置初始密码，需要进行md5加密
        String password = DigestUtils.md5DigestAsHex("123456".getBytes());
        employee.setPassword(password);
        return this.save(employee);
    }

    /**
     * 员工管理-判断用户名是否存在
     * @param username
     * @return
     */
    @Override
    public boolean selectEmpByUsername(String username) {
        LambdaQueryWrapper<Employee> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Employee::getUsername,username);
        Employee employee = this.getOne(wrapper);
        if (employee == null) {
            return false;
        }
        return true;
    }

    /**
     * 员工管理-分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @Override
    public Page<Employee> selectEmpByPageAndCondition(Integer page, Integer pageSize, String name) {
        //构造分页构造器
        Page<Employee> pageInfo = new Page<>(page,pageSize);
        //构造条件构造器
        LambdaQueryWrapper<Employee> wrapper = new LambdaQueryWrapper<>();
        //添加过滤条件 --> 不为空才添加
        wrapper.like(StringUtils.isNotEmpty(name),Employee::getName,name);
        //添加排序条件
        wrapper.orderByDesc(Employee::getUpdateTime);
        //查询并返回结果
        return this.page(pageInfo, wrapper);
    }

    /**
     * 员工管理-修改员工信息(包括状态-->启用/禁用)
     * @param employee
     * @return
     */
    @Override
    public boolean updateEmp(Employee employee) {
        return this.updateById(employee);
    }

    /**
     * 员工管理-根据id查找员工对象
     * @param id
     * @return
     */
    @Override
    public Employee getEmpById(Long id) {
        return this.getById(id);
    }
}
